﻿package com.danielsheldon.ui
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	public class AnimatedRollOver extends MovieClip
	{

		private var rewind:Boolean = true;

		public function AnimatedRollOver()
		{
			stop();
			buttonMode = true;
			addEventListener(MouseEvent.ROLL_OVER, hover);
			addEventListener(MouseEvent.ROLL_OUT, hover);
		}

		private function hover(e:MouseEvent)
		{
			if (e.type=="rollOver")
			{
				rewind = false;
			} else
			{
				rewind = true;
			}
			addEventListener(Event.ENTER_FRAME, animate);
		}

		private function animate(e:Event)
		{
			if (rewind)
			{
				prevFrame();
			} else
			{
				nextFrame();
			}
			if (currentFrame == 1 || currentFrame == totalFrames)
			{
				removeEventListener(Event.ENTER_FRAME, animate);
			}
		}
	}
}